<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
    >
      <template slot="menuLeft">
        <el-button
          style="margin-right: 20px"
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="add"
        >
          添加
        </el-button>
      </template>
      <template
        slot="menu"
        slot-scope="scope"
      >
        <el-button
          type="text"
          size="small"
          @click="view(scope.row, scope.index)"
        >
          查看
        </el-button>
        <el-button
          v-if="[ARTICLE_STATUS.DRAFT, ARTICLE_STATUS.SHIELD].includes(scope.row.status) "
          type="text"
          size="small"
          @click="edit(scope.row, scope.index)"
        >
          编辑
        </el-button>
        <el-button
          type="text"
          size="small"
          @click="handleDelete(scope.row, scope.index)"
        >
          删除
        </el-button>
        <el-button
          v-if="scope.row.status === ARTICLE_STATUS.DRAFT"
          type="text"
          size="small"
          @click="publish(scope.row, true)"
        >
          发布
        </el-button>
        <el-button
          v-if="[ARTICLE_STATUS.PUBLISHED].includes(scope.row.status)"
          type="text"
          size="small"
          @click="shield(scope.row)"
        >
          屏蔽
        </el-button>
        <el-button
          v-if="[ARTICLE_STATUS.SHIELD].includes(scope.row.status)"
          type="text"
          size="small"
          @click="publish(scope.row, false)"
        >
          恢复
        </el-button>
        <el-button
          v-if="[ARTICLE_STATUS.PUBLISHED,ARTICLE_STATUS.SHIELD].includes(scope.row.status)"
          type="text"
          size="small"
          @click="comment(scope.row, scope.index)"
        >
          评论
        </el-button>
      </template>
    </avue-crud>
    <addPopup
      :id="id"
      v-model="addVisible"
      :action="action"
      @success="(val) => getList(page, val)"
    ></addPopup>
    <commentPopup
      :id="id"
      v-model="commentVisible"
    ></commentPopup>
  </basic-container>
</template>

<script>
import {
  fetchList,
  delObj,
  publishObj,
  shieldObj,
} from '@/api/core/articleManage';
import { tableOption } from '@/const/crud/content/article-manage';
import { mapGetters } from 'vuex';
import crudMixins from '@/mixins/crud';
import { ARTICLE_STATUS } from '@/const/type';
import addPopup from './components/addPopup.vue';
import commentPopup from './components/commentPopup.vue';

export default {
  name: 'ArticleManage',
  components: {
    addPopup,
    commentPopup,
  },
  mixins: [crudMixins],
  data() {
    return {
      form: {},
      isAudit: false,
      ARTICLE_STATUS,
      radioValue: ARTICLE_STATUS.REJECTED,
      loading: false,
      addVisible: false,
      id: null,
      action: '',
      commentVisible: false,
    };
  },
  computed: {
    ...mapGetters(['permissions']),
    tableOption() {
      return tableOption();
    },
  },
  watch: {},
  methods: {
    comment(row) {
      this.id = row.id;
      this.commentVisible = true;
    },
    add() {
      this.id = null;
      this.action = 'add';
      this.addVisible = true;
    },
    view(row) {
      this.id = row.id;
      this.action = 'view';
      this.addVisible = true;
    },
    edit(row) {
      this.id = row.id;
      this.action = 'edit';
      this.addVisible = true;
    },
    addTrace(row) {
      this.id = row.id;
      this.traceVisible = true;
    },
    sign(row) {
      this.id = row.id;
      this.signVisible = true;
    },
    publish(row, flag) {
      const tip = flag ? '发布' : '恢复发布';
      this.$confirm(
        `${tip}后该文章将展示在移动用户端，是否确定${tip}该篇文章？`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(async () => {
        await publishObj(row.id);
        this.$message.success('操作成功');
        this.getList(this.page, true);
      });
    },
    shield(row) {
      this.$confirm(
        '屏蔽后该文章将取消展示在移动用户端，是否确定屏蔽该篇文章？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(async () => {
        await shieldObj(row.id);
        this.$message.success('操作成功');
        this.getList(this.page, true);
      });
    },
    // 删除
    handleDelete: function (row, index) {
      this.$confirm('删除后该文章将无法恢复，是否确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        await delObj(row.id);
        this.$message.success('删除成功');
        this.getList(this.page, true);
      });
    },
    // 列表查询
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      const timeObj = {};
      if (this.searchForm.createTime && this.searchForm.createTime.length) {
        timeObj.createTimeStart = this.searchForm.createTime[0];
        timeObj.createTimeEnd = this.searchForm.createTime[1];
        // delete this.searchForm.createTime;
      }
      fetchList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          this.searchForm,
          timeObj
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
          this.tableData.forEach((el) => {
            el.label = JSON.parse(el.label);
          });
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
  },
};
</script>
